package com.javaclimb.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.javaclimb.drug.common.ResultMapUtil;
import com.javaclimb.drug.entity.Billinfo;
import com.javaclimb.drug.service.IBillinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description TODO 账单信息相关的controller
 * @createTime 2021年10月02日 09:05:00
 */

@Controller
@RequestMapping(value = "/billinfo")
public class BillinfoController {
    @Autowired
    private IBillinfoService iBillinfoService;


    /**
     * 转向账单信息页面
     * @return
     */
    @RequestMapping
    public String login(){
        return "/billinfo";
    }

    /**
     * 分页查询账单信息列表
     * @return
     */
    @RequestMapping(value = "/billinfoQueryPage")
    @ResponseBody
    public HashMap<String, Object> billinfoQueryPage(String param, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        try {
            IPage<Billinfo> iPage = iBillinfoService.selectBillinfoPage(page, limit, param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向账单信息新增页面
     * @return
     */
    @RequestMapping(value = "/billinfoPage")
    public String billinfoPage(){
        return "/billinfoPage";
    }

    /**
     * 添加一个账单信息
     * @return
     */
    @RequestMapping(value = "/billinfoAdd")
    @ResponseBody
    public Object billinfoAdd(Billinfo billinfo){
        try {
//            billinfo(new Date());
            int i = iBillinfoService.addBillinfo(billinfo);

            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向账单信息编辑页面
     * @return
     */
    @RequestMapping(value = "/billinfoQueryById")
    public String billinfoQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Billinfo billinfo = iBillinfoService.queryBillinfoById(id);
        model.addAttribute("obj",billinfo);

        return "/billinfoPage";
    }
    /**
     * 修改一个账单信息
     * @return
     */
    @RequestMapping(value = "/billinfoEdit")
    @ResponseBody
    public Object billinfoEdit(Billinfo billinfo){
        try {
            int i = iBillinfoService.editBillinfo(billinfo);

            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
    /**
     * 删除一个账单信息
     * @return
     */
    @RequestMapping(value = "/billinfoDelById")
    @ResponseBody
    public Object billinfoDelById(Integer id){
        try {
            int i = iBillinfoService.delBillinfoById(id);

            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

}
